<template>
	<view :modelValue="sample" class="container" >
		<!-- 导航栏 -->
		<uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar left-icon="left" left-text="返回"
			titleStyle="{height: '52px'}" :title="sample.name" @clickLeft="back">
			<block slot="right">
				<view class="city">
					<text v-show="enableDelegateBtn" style="font-size: 15px;" @click="confirmDelegate">确认委托</text>
				</view>
			</block>
		</uni-nav-bar>
		
		<uni-section title="委托归还信息" type="line">
			<view class="example">
				<uni-forms-item label="领样人" labelPosition="top">
					<uni-easyinput v-model="sample.foreHandlerName" disabled=""/>
				</uni-forms-item>
				
				<uni-forms-item label="指定委托人" labelPosition="top">
					<uni-combox :candidates="userNames" v-on:input="userChanged" placeholder="请选择委托人"
						v-model="sample.returneeName"></uni-combox>
				</uni-forms-item>
			</view>
		</uni-section>
		<u-modal :show="showConfirmModal" title="委托确认" :showCancelButton="true" 
			 :content="'将样品: ['+sample.name+']\n委托给: ['+sample.returneeName+'] 归还?'" @cancel="closeModal"
			 @confirm="confirm" :asyncClose="false">
		</u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				enableDelegateBtn:false,
				showConfirmModal:false,
				selectedOrgName: '',
				selectedUserName: '',
				orgNames: [],
				orgs: [],
				userNames: [],
				users: [],
				sample:{}
			}
		},
		onLoad(option) {
			// 加载样品存放地点(字典数据，Location)
			this.loadSampleInfo(option.id);
		},
		computed: {},
		methods: {
			loadSampleInfo(sampleId){
				var this_ = this;
				this.$u.api.querySampleInfo(sampleId).then(res=>{
					this_.sample = res.data;
					//继续流转时,默认先将领取人人设置为空
					this_.sample.returneeName = ''
					this_.sample.returnee = ''
					this_.queryUserList();
				});
			},
			confirm(){
				var this_ = this; //存储this
				this.$u.api.forwardSample({'id':this_.sample.id,'returnee':this_.sample.returnee}).then(res=>{
					this_.$u.toast('委托成功');
					this_.back();
				});
			},
			closeModal(){
				this.showConfirmModal = false;
			},
			confirmDelegate(){
				if(!this.enableDelegateBtn) {
					return;
				}
				var this_ = this; //存储this 
				this_.showConfirmModal = true; 
			},
			userChanged(userName) {
				//如果填入的用户名称非法，则主动清空内容
				var userId = this.resolveUserIdWithSelectedUserName(userName);

				if (!userId) {
					this.sample.returneeName='';
				//只有选择用户不为空,且不为当前用户,则显示继续流转按钮
				} else if(userId != this.sample.returnee){
					this.sample.returnee = userId;
					this.enableDelegateBtn = true;
				}
			},
			resolveUserIdWithSelectedUserName(userName) {
				for (let index = 0; index < this.users.length; index++) {
					if (this.users[index].userName === userName) return this.users[index].userId;
				}
				return null;
			},
			queryUserList(params) {
				var this_ = this; //存储this
				var deptId = this_.sample.consumeDep?this_.sample.consumeDep:this_.$store.state.vuex_user.deptId;
				this.$u.api.queryUserList().then((response) => {
					//这个控件只能支持字符串选取
					this_.users = response.rows;
					let userNames = [response.rows.length];
					let newUserNames = [response.rows.length];
					for (let index = 0; index < response.rows.length; index++) {
						userNames[index] = response.rows[index].nickName;
					}
					this.newUserNames = newUserNames;
					this_.userNames = userNames;
				});
			},
			back() {
				this.backPageRefresh();
			}
		}
	}
</script>

<style>
	.uni-section {
		margin-top: 0px !important;
		background-color: #eee;
	}

	.custom-image-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.example {
		background-color: white;
		padding: 10px 20px 15px 20px;
	}

	.segmented-control {
		margin-bottom: 10px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
</style>
